package com.michaelbulava.TFS.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import com.michaelbulava.TFS.TFSVcsModuleComponent;
import com.michaelbulava.TFS.vcs.TFSVcs;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MichaelA on 8/20/2015.
 */
public class GoOfflineAction extends OnlineRequiredAction {
    /**
     * Implement this method to provide your action handler.
     *
     * @param e Carries information on the invocation place
     */
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        final VirtualFile[] files = VcsUtil.getVirtualFiles(e);
        //List<Module> modules = new ArrayList<>();
        List<Module> modules = new ArrayList<>();

        for(VirtualFile file : files){
            Module module = TFSFileUtil.getModuleForFile(file, project);
            if (!modules.contains(module))
                modules.add(module);
        }
        for(Module module : modules){
            TFSVcsModuleComponent moduleComponent = TFSVcsModuleComponent.getInstance(module);
            TFSVcs.registerModuleForTFSOffline(module);
            moduleComponent.setOffline(true);
        }
    }
}
